package com.you.swagger.service.impl;

import com.you.swagger.controller.IUserInterface;
import com.you.swagger.controller.ProxyUser;
import com.you.swagger.controller.UserInterfaceImplDump;
import com.you.swagger.util.MyClassLoader;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author yough
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2021/1/12
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        //IUserInterface userInterface = (IUserInterface) new ProxyUser().getProxy();
        try {
            //加载字节码文件
            MyClassLoader myClassLoader = new MyClassLoader();
            Class<?> clazz = myClassLoader.defineClass("com/you/swagger/controller/UserInterfaceImpl",
                    UserInterfaceImplDump.dump());
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(MyFactoryBean.class);
            registry.registerBeanDefinition("userInterface", builder.getBeanDefinition());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//        IUserInterface userInterface = (IUserInterface) new ProxyUser().getProxy();
//        beanFactory.registerSingleton("userInterface", userInterface);
    }
}
